# -*- coding: utf-8 -*-
"""
@Time ： 2021/11/23 20:53
@Auth ： Mr. William 1052949192
@Company ：特斯汀学院 @testingedu.com.cn
@Function ：主流程
登录-上传头像-新增收货地址-搜索-加购物车-下单-取消订单
"""
import time
from time import sleep

from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select

from keywords.webkeys import Web

web = Web()
web.openbrowser('Chrome')

# 登录
web.geturl('http://testingedu.com.cn:8000/index.php/Home/user/login.html')
web.input('//input','13800138006')
web.input('//input[@id="password"]','1234561')
web.getverify('//*[@id="verify_code_img"]')
web.input('//*[@name="verify_code"]','{verify}')
web.sleep(3)
web.jsclick('//a[@class="J-login-submit"]')
web.sleep(1)
print(web.gettext('//a[@class="red userinfo"]'))
web.assertcontains('blue','{text}')

# 可以使用(//a[text()="个人信息"])[2]获取第几个元素
web.click('(//a[text()="个人信息"])[2]')
web.click('//*[@id="preview"]')
web.sleep(1)

web.intoiframe('//iframe')

web.input('//*[@id="filePicker"]/div[2]/input',
          r'D:\PythonPro\VIPPRO\VipFrame10\lib\will.png')
web.sleep(1)
web.click('//div[@class="saveBtn"]')

# 新增地址
web.geturl('http://testingedu.com.cn:8000/index.php/Home/User/address_list.html')
web.click('//span[text()="增加新地址"]')
web.input('//input[@name="consignee"]','老will')
web.input('//input[@name="mobile"]','18888888888')
# ele = web.driver.find_element(By.XPATH,'//select[@id="province"]')
# select = Select(ele)
# select.select_by_index(1)
# sleep(2)
# select.select_by_value('338')
# sleep(2)
# select.select_by_visible_text('湖南省')

web.select('//select[@id="province"]','湖南省')
web.select('//select[@id="city"]','长沙市')
web.select('//select[@id="district"]','岳麓区')
web.select('//select[@id="twon"]','岳麓街道')
web.input('//input[@name="address"]','dsalkjfoiweqjfklsdjfk')
web.click('//a[@id="address_submit"]')

# 删除收货地址
web.sleep(1)
web.click('//span[text()="老will"]/../../li[4]/a[2]')
web.sleep(1)

# 搜索
web.input('//input[@id="q"]','手机')
web.click('//a[text()="搜索"]')

# 加购物车
web.click('//a[contains(text(),"荣耀 畅玩")]/../../div[5]/div[2]/a')
web.sleep(1)
web.click('//a[text()="加入购物车"]')
web.sleep(1)
web.click('//span[@class="layui-layer-setwin"]/a')

# ele = web.driver.find_element(By.XPATH,'//span[text()="我的购物车"]')
#
# # 使用actions移动鼠标到元素
# actions = ActionChains(web.driver)
# actions.move_to_element(ele)
# # 使actions的所有操作生效
# actions.perform()
web.sleep(1)
# 前置条件
web.moveto('//span[text()="我的购物车"]')
web.click('//a[text()="去购物车结算 >>"]')

web.click('//a[text()="去结算"]')
web.click('//button[contains(text(),"提交订单")]')

web.gettext('//p[@class="succ-p"]',r'\d{18}')

web.click('//a[text()="我的订单"]')
web.sleep(1)
# print(web.driver.window_handles)
# # 通过下标获取窗口id，进行切换
# web.driver.switch_to.window(web.driver.window_handles[1])
web.switchwin('我的订单')

web.input('//*[@id="search_key"]',"{text}")
web.click('//*[@id="search_order"]/input[2]')